﻿using UnityEngine;
using System;
using System.Collections;

namespace Hont
{
    public static class GameDateHelper
    {
        public static string GameTimeSpanToString(GameTimeSpan timeSpan)
        {
            if (timeSpan.Hours == 0) return string.Format("{0:00}:{1:00}", timeSpan.Minutes, timeSpan.Seconds);
            else if (timeSpan.Days == 0) return string.Format("{0:00}:{1:00}", timeSpan.Hours, timeSpan.Minutes);

            return string.Format("{0}.{1:00}:{2:00}", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes);
        }

        public static GameDateTime GetThisDayLastTime(GameDateTime dateTime)
        {
            var hour = GameDateStandard.Instance.HourPerDay - 1;
            var minute = GameDateStandard.Instance.MinutePerHour - 1;
            var second = GameDateStandard.Instance.SecondPerMinute - 1;
            var millisSecond = GameDateStandard.Instance.MillisPerSecond - 1;

            return new GameDateTime(dateTime.Year, dateTime.Month, dateTime.Day, hour, minute, second, millisSecond);
        }

        /// <summary>
        /// Get this week on saturday.
        /// </summary>
        public static GameDateTime GetThisWeekLastTime(GameDateTime dateTime)
        {
            var current = dateTime.DayOfWeek;
            var sub = (int)DayOfWeek.Saturday - current;

            var hour = GameDateStandard.Instance.HourPerDay - 1;
            var minute = GameDateStandard.Instance.MinutePerHour - 1;
            var second = GameDateStandard.Instance.SecondPerMinute - 1;
            var millisSecond = GameDateStandard.Instance.MillisPerSecond - 1;

            var result = new GameDateTime(dateTime.Year, dateTime.Month, dateTime.Day, hour, minute, second, millisSecond);

            result = result.AddDays(sub);

            return result;
        }

        public static GameDateTime GetThisMonthLastTime(GameDateTime dateTime)
        {
            var currentMonth = dateTime.Month;
            var days = GameDateStandard.Instance.DaysToMonth[currentMonth]
                - GameDateStandard.Instance.DaysToMonth[Mathf.Max(currentMonth - 1)];

            var hour = GameDateStandard.Instance.HourPerDay - 1;
            var minute = GameDateStandard.Instance.MinutePerHour - 1;
            var second = GameDateStandard.Instance.SecondPerMinute - 1;
            var millisSecond = GameDateStandard.Instance.MillisPerSecond - 1;

            return new GameDateTime(dateTime.Year, currentMonth, days, hour, minute, second, millisSecond);
        }

        /// <summary>
        /// Get from origin to the current year in total month.
        /// </summary>
        public static int GetTotalMonth(GameDateTime dateTime)
        {
            var a = dateTime.Year - 1;
            var b = a * GameDateStandard.Instance.MonthPerYear;
            var c = dateTime.Month;

            return b + c;
        }
    }
}
